package com.spn.blog.api.service;

import com.alibaba.fastjson.JSONObject;
import com.spn.blog.api.config.RedisKey;
import com.spn.blog.api.utils.MessageUtils;
import com.spn.blog.common.dao.pojo.LoginUser;
import com.spn.blog.common.dao.pojo.SysUser;
import com.spn.blog.common.utils.JWTUtils;
import com.spn.blog.common.utils.RedisCacheUtil;
import com.spn.blog.common.vo.Result;
import com.spn.blog.common.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

@Component
//前端连接的地址路径
@ServerEndpoint("/api/ws/getFriends/{token}")
@Slf4j
public class ChatEndpoint {
    //与某个客户端的连接会话，需要通过它来给客户端发送数据
    private Session session;
    private String key;

    RedisCacheUtil redisCacheUtil = (RedisCacheUtil)RedisCacheUtil.getBean("redisCacheUtil");

    private static CopyOnWriteArraySet<ChatEndpoint> webSockets =new CopyOnWriteArraySet<>();
    // 用来存放每个客户端对应的WebSocket对象。若要实现服务端与单一客户端通信的话，可以使用Map来存放，其中Key可以为用户标识
    private static Map<String,Session> sessionPool = new ConcurrentHashMap<>();

    @OnOpen
    public void onOpen(Session session, @PathParam(value="token") String token) {
        this.key = token;
        // 所有都是好友，只要有人连接进来，就把他加入到map中，userId 作为key
        this.session = session;
        webSockets.add(this);
        sessionPool.put(token, session);
        log.info("【websocket消息】连接进行连接，总数为:" + webSockets.size());
    }

    @OnClose
    public void onClose(Session session) {
        webSockets.remove(this);
        sessionPool.remove(key);

        log.info("【websocket消息】连接断开，总数为:" + webSockets.size());
    }

    @OnMessage
    public void onMessage(String message) {
        log.info("【websocket消息】收到客户端消息:" + message);
    }

    // 此为广播消息
    public void sendAllMessage(String message) {
        log.info("【websocket消息】广播消息:" + message);
        for(ChatEndpoint webSocket : webSockets) {
            try {
                webSocket.session.getAsyncRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    // 此为单点消息 (发送文本)
    public void sendTextMessage(String key, String message) {
        Session session = sessionPool.get(key);
        if (session != null) {
            try {
                session.getBasicRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
